package stu.cn.ua.rgr;
import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import queues.QueueForTransactions;
import queues.TransProcessQueue;
import stat.Histo;
import widgets.ChooseRandom;
import stu.cn.ua.rgr.Model;
import transProcess.ITransProcesable;

public class Sivalka extends Actor {
	
	private Main gui;
	private Model model;
	private QueueForTransactions queueOfSivalka;
	private int sizeOfSivalka;
	private boolean load;
	private double finishTime;
	private ChooseRandom rnd;
	private ChooseRandom rndUp;
	private Histo histo55;
	private TransProcessQueue queueSivalka;
	
	public TransProcessQueue getQueueSivalka(){
		return queueSivalka;
	}
	public void setQueueSivalka(TransProcessQueue queueSivalka){
		this.queueSivalka=queueSivalka;
	}
	public Sivalka(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		this.gui = gui;
		this.model = model;
		this.queueOfSivalka = model.getQueueOfSivalka();
		this.sizeOfSivalka = Integer.parseInt(gui.getJTextCntSiv().getText());
		finishTime=Double.parseDouble(gui.getJTextTime().getText());
		this.rnd = gui.getChooseRandomSivTime(); //время засева
		this.rndUp = gui.getChooseRandomGrUpload();
		this.histo55 = model.getHisto2();
		
	}
	
	@Override
	protected void rule() {
		getDispatcher().printToProtocol("Сівалка почала роботу ");
		IWaitCondition full = new IWaitCondition() {
			
			@Override
			public boolean testCondition() {
				
				return load;
			}
			public String toString() {
				return " полная";	
			}
		};
		
		while (getDispatcher().getCurrentTime()<finishTime) {
			getDispatcher().printToProtocol(this + " стала в чергу на завантаження");
			load=false;
			queueOfSivalka.addLast(this);
			//holdForTime(rndUp.next());
			double time1 = getDispatcher().getCurrentTime();
			try {//становимся в очередь на загрузку
			
				waitForCondition(full);
			} catch (DispatcherFinishException e) {
				return;
			}
			histo55.add(getDispatcher().getCurrentTime()-time1);
			getDispatcher().printToProtocol(this + " почала сіяти");
			//начинаем сееть
			holdForTime(rnd.next());
		
		}
	}
	public void setLoad(){
		load=true;
	}
}
